package com.dhcc.bpm.modules.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.MessageUtils;
import com.dhcc.bpm.common.utils.SafeHtmlUtils;
import com.dhcc.bpm.modules.content.entity.Message;
import com.dhcc.bpm.modules.content.service.IMessageService;
import com.dhcc.bpm.modules.content.vo.MessageVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName MessageController
 * @Description  推送消息表 控制器
 * @Author daina
 * @Date 2020/3/9 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-content/message")
@Api(value = "推送消息表", tags = "推送消息表接口")
public class MessageController extends BladeController {
	private IMessageService messageService;

	private ISerialNumberService serialNumberService;



	/**
	 * @Description 消息通知详情
	 * @Author daina
	 * @Date 15:44 2020/3/12
	 * @param message
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.content.entity.Message>
	 **/
	@ApiLog("消息通知模块表详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Log(title = "消息通知详情", businessType = BusinessType.SELECT)
	@ApiOperation(value = "消息通知详情", notes = "传入message")
	public R<Message> detail(Message message) {
		Message detail = messageService.getOne(Condition.getQueryWrapper(message));
		return R.data(detail);
	}

	/**
	 * @Description 分页 推送消息表
	 * @Author daina
	 * @Date 15:44 2020/3/12
	 * @param message
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.content.entity.Message>>
	 **/
	@ApiLog("消息通知模块表列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "消息通知分页", notes = "传入message")
	@Log(title = "消息通知分页", businessType = BusinessType.SELECT)
	public R<IPage<Message>> list(Message message, Query query) {
		IPage<Message> pages = messageService.page(Condition.getPage(query), Condition.getQueryWrapper(message));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 推送消息表
	 * @Author daina
	 * @Date 15:44 2020/3/12
	 * @param message
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.content.vo.MessageVO>>
	 **/
	@ApiLog("消息通知模块表分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "消息通知分页", notes = "传入message")
	@Log(title = "消息通知分页", businessType = BusinessType.SELECT)
	public R<IPage<MessageVO>> page(MessageVO message, Query query) {
		IPage<MessageVO> pages = messageService.selectMessagePage(Condition.getPage(query), message);
		return R.data(pages);
	}

	/**
	 * @Description 新增 推送消息表
	 * @Author daina
	 * @Date 15:43 2020/3/12
	 * @param message
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消息通知模块表新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增消息通知", notes = "传入message")
	@Log(title = "新增消息通知", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody Message message) {
		String safeHtml = SafeHtmlUtils.getSafeHtml(message.getMsgContent());
		message.setMsgContent(safeHtml);
		return R.status(messageService.save(message));
	}

	/**
	 * @Description 修改 推送消息表
	 * @Author daina
	 * @Date 15:43 2020/3/12
	 * @param message
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消息通知模块表更新")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改消息通知", notes = "传入message")
	@Log(title = "修改消息通知", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody Message message) {
		String safeHtml = SafeHtmlUtils.getSafeHtml(message.getMsgContent());
		message.setMsgContent(safeHtml);
		return R.status(messageService.updateById(message));
	}

	/**
	 * @Description 新增或修改 推送消息表
	 * @Author daina
	 * @Date 15:43 2020/3/12
	 * @param message
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消息通知模块表提交")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改消息通知", notes = "传入message")
	@Log(title = "新增或修改消息通知", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody Message message) {
		if (message.getId() == null){
			String msgCode = serialNumberService.generateSerialNumberByModelCode("opc_access_control");
			message.setMsgCode(msgCode);
			message.setStatus(0);
		}
		message.setMsgName(message.getMsgName().trim());
		message.setPushStatus(1);
		try {
			//空指针处理 修改时间 20200914 17:02
			if(message.getMsgContent() != null){
				String safeHtml = SafeHtmlUtils.getSafeHtml(message.getMsgContent());
				message.setMsgContent(safeHtml);
			}
			return R.status(messageService.saveOrUpdate(message));
		} catch (Exception e) {
			if (e.getMessage().contains("for key 'message_name_idx'") || e.getMessage().contains("ORA-00001")) {
				return R.fail(500, "该消息名称已存在");
			}
			return R.fail(500, "数据库异常");
		} finally {

		}
	}
	/**
	 * @Description 消息通知（个人推送）
	 * @Author daina
	 * @Date 14:02 2020/3/26
	 * @param message
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消息通知个人")
	@PostMapping("/pushMessagePer")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改消息通知", notes = "传入message")
	@Log(title = "新增或修改消息通知", businessType = BusinessType.INSERT)
	@Transactional(rollbackFor = Exception.class)
	public R submitMessage(@Valid @RequestBody Message message) {
		if (message.getId() == null){
			String msgCode = serialNumberService.generateSerialNumberByModelCode("opc_access_control");
			message.setMsgCode(msgCode);
			message.setStatus(0);
			// 消息类型 个人
			message.setMsgType("1");
			// 消息接收人
			message.setReceiver(message.getReceiver());
			// 消息内容
			message.setMsgContent(message.getMsgContent());
			// 消息名称
			message.setMsgName(message.getMsgName());
		}
		message.setPushStatus(1);
		try {
			String safeHtml = SafeHtmlUtils.getSafeHtml(message.getMsgContent());
			message.setMsgContent(safeHtml);
			messageService.saveOrUpdate(message);
			// 调用消息推送
			Message messageEntity = messageService.getById(message.getId());
			MessageUtils utils = new MessageUtils();
			utils.sendMessage("开放平台",messageEntity.getId().toString());
			messageEntity.setPushStatus(2);
			return R.status(messageService.saveOrUpdate(messageEntity));

		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			if (e.getMessage().contains("for key 'message_name_idx'") || e.getMessage().contains("ORA-00001")) {
				return R.fail(500, "该消息名称已存在");
			}
			return R.fail(500, "数据库异常");
		} finally {

		}

	}
	/**
	 * @Description 推送消息
	 * @Author daina
	 * @Date 16:01 2020/3/19
	 * @param id
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("推送消息")
	@PostMapping("/pushMessage")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "推送消息", notes = "传入message")
	@Log(title = "推送消息", businessType = BusinessType.OTHER)
	public R pushMessage(@ApiParam(value = "主键", required = true) @RequestParam String id) {
		Message messageEntity = messageService.getById(id);
		MessageUtils utils = new MessageUtils();
		utils.sendMessage(")开放平台",messageEntity.getId().toString());
		messageEntity.setPushStatus(2);
		return R.status(messageService.saveOrUpdate(messageEntity));
	}

	/**
	 * @Description 删除 推送消息表
	 * @Author daina
	 * @Date 15:43 2020/3/12
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消息通知模块表删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除消息通知", notes = "传入ids")
	@Log(title = "删除消息通知", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(messageService.deleteLogic(Func.toLongList(ids)));
	}

	
}
